/**
 * Created by jianbo on 2016/3/24.
 */
define(['app'],function (app) {
  app.controller('companySimpleInforController',companySimpleInforController);
  companySimpleInforController.$inject = ['globalConfig','$scope','underscoreJS','accountService','$validation','$state'];

  function companySimpleInforController(globalConfig,$scope,underscoreJS,accountService,$validationProvider,$state){

    $scope.getCompanySimpleInfor = _getCompanySimpleInfor;
    $scope.checkValid = $validationProvider.checkValid;


    function _getCompanySimpleInfor(){
      var companyId = this.vm.companyId;
      var Company = accountService.getCompanySimpleInfor(companyId);
      Company.then(_addCompanyAccountSuccess,_addCompanyAccountFail);
      return Company;
    }
    function _addCompanyAccountSuccess(data){
      globalConfig.rootScope.errorMessage = '';
      var company = data.company.id;
      underscoreJS.delay(function(){$state.go('access.signup', {data: company})},3000);
    }

    function _addCompanyAccountFail(err){
      if (err) {
        globalConfig.rootScope.successMessage = "";
        globalConfig.rootScope.errorMessage = err.message.join('&lt;br&frasl;&gt;');
      }
    }
  }
  app.controller('signinController',signinController);
  signinController.$inject = ['$scope','$state','cryptoJS','authService','$validation','currentUserService'];

  function signinController($scope,$state,cryptoJS,authService,$validationProvider,currentUserService){

    $scope.viewBag = [];
    $scope.checkValid = $validationProvider.checkValid;
    $scope.signIn = function(username, password){
      var a =  authService.signIn(username, cryptoJS.MD5(password).toString());
      a.then(verification ,signIn_Fail);
      return a;
    };

    function verification(){
      var verificationMe = currentUserService.currentUser();
      verificationMe.then(
        function(data){
          /*判断是否第一次登录并激活*/
          if(data.firstlogin){
            $state.go('access.resetInfo');
          }else{
            signIn_Success();
          }
        },signIn_Fail
      )
    }

    var signIn_Success = function(){
      $state.go('home');
    };

    var signIn_Fail = function(err){
      if (err) {
        $scope.viewBag.error = err.message.join('<br/>');
        console.info(err.message.join('<br/>'));
      }
    }

  }
  app.controller('signupController',signupController);
  signupController.$inject = ['globalConfig','$scope','underscoreJS','accountService','$validation','$state','cryptoJS','$stateParams'];

  function signupController(globalConfig,$scope,underscoreJS,accountService,$validationProvider,$state,cryptoJS,$stateParams){

    $scope.vm = {
      departmentId :'',
      name: '',
      Phone: '',
      password: '',
      smsCode: '',
      securityCode: '',
      agree: ""
    };

    $scope.viewBag = {
      securityCodeError: false
    };

    /*初始化获取当前公司*/
    paramCompany();
    $scope.requestAddMember = _addCompanyMember;
    $scope.lastPase = _lastPase;
    $scope.checkValid = $validationProvider.checkValid;

    function _lastPase(){
      /*上一步*/
      $state.go('access.getCompanySimpleInfor');
    }

    /*获取公司*/
    function paramCompany(){
      var companyId = $stateParams.data;
      /*getCompanySimpleInfor的http请求已缓存*/
      var Company = accountService.getCompanySimpleInfor(companyId);
      Company.then(function(data){
        Company = data;
        $scope.company = data.company;
      },_addCompanyAccountFail);
    }

    /**
     * 清求注册成员
     * @private
     */
    function _addCompanyMember(vm){

      vm.companyId = $scope.company.id;
      vm.password = cryptoJS.MD5(this.vm.initialPassword).toString();

      var member = accountService.signUp(vm);
      member.then( _addCompanyAccountSuccess,_addCompanyAccountFail);

      return member;
    }

    function _addCompanyAccountSuccess(data){
      globalConfig.rootScope.errorMessage = '';
      globalConfig.rootScope.successMessage = "成功发送申请，请等待管理员["+$scope.company.adminName+"]审批";
      underscoreJS.delay(function(){$state.go('access.signin');},3000);
    }

    function _addCompanyAccountFail(err){
      if (err) {
        globalConfig.rootScope.successMessage = '';
        globalConfig.rootScope.errorMessage = err.message.join('&lt;br&frasl;&gt;');
      }
    }
  }

});
